package com.thon.service.project.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.project.ProjectScheduleDao;
import com.thon.entity.project.ProjectSchedule;
import com.thon.service.project.ProjectScheduleService;

/**
 * @file ProjectScheduleServiceImpl.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月13日 下午3:26:24
 * @description: TODO
 */
@Service
@Transactional
public class ProjectScheduleServiceImpl implements ProjectScheduleService {

	@Autowired
	private ProjectScheduleDao projectScheduleDao;

	@Override
	public List<ProjectSchedule> listProjectSchedules(int projectId) {

		return projectScheduleDao.listProjectSchedules(projectId);
	}

	@Override
	public void addSchedule(ProjectSchedule projectSchedule) {

		projectScheduleDao.save(projectSchedule);
	}

	@Override
	public void updateSchedule(ProjectSchedule projectSchedule) {

		projectScheduleDao.update(projectSchedule);
	}

	@Override
	public void deleteScheduleById(int id) {

		ProjectSchedule projectSchedule = projectScheduleDao.get(id);
		int pid = projectSchedule.getProject().getId();
		projectScheduleDao.delete(id);
		List<ProjectSchedule> projectSchedules = projectScheduleDao.listProjectSchedules(pid);
		for (int i = 0; i < projectSchedules.size(); i++) {
			ProjectSchedule ps = projectSchedules.get(i);
			ps.setStage(i + 1);
			projectScheduleDao.update(ps);
		}
	}

	@Override
	public ProjectSchedule getProjectSchedule(Integer stage, Integer projectId) {
		return projectScheduleDao.getProjectSchedule(stage, projectId);
	}

	@Override
	public ProjectSchedule getProjectSchedule(int id) {
		return projectScheduleDao.get(id);
	}

}
